package com.healthyeating.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.healthyeating.common.annotation.Log;
import com.healthyeating.common.core.controller.BaseController;
import com.healthyeating.common.core.domain.AjaxResult;
import com.healthyeating.common.enums.BusinessType;
import com.healthyeating.manage.domain.Drink;
import com.healthyeating.manage.service.IDrinkService;
import com.healthyeating.common.utils.poi.ExcelUtil;
import com.healthyeating.common.core.page.TableDataInfo;

/**
 * 饮水记录Controller
 * 
 * @author 段奇乐
 * @date 2024-12-20
 */
@RestController
@RequestMapping("/manage/drink")
public class DrinkController extends BaseController
{
    @Autowired
    private IDrinkService drinkService;

    /**
     * 查询饮水记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:drink:list')")
    @GetMapping("/list")
    public TableDataInfo list(Drink drink)
    {
        startPage();
        List<Drink> list = drinkService.selectDrinkList(drink);
        return getDataTable(list);
    }

    /**
     * 导出饮水记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:drink:export')")
    @Log(title = "饮水记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Drink drink)
    {
        List<Drink> list = drinkService.selectDrinkList(drink);
        ExcelUtil<Drink> util = new ExcelUtil<Drink>(Drink.class);
        util.exportExcel(response, list, "饮水记录数据");
    }

    /**
     * 获取饮水记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:drink:query')")
    @GetMapping(value = "/{waterId}")
    public AjaxResult getInfo(@PathVariable("waterId") Long waterId)
    {
        return success(drinkService.selectDrinkByWaterId(waterId));
    }

    /**
     * 新增饮水记录
     */
    @PreAuthorize("@ss.hasPermi('manage:drink:add')")
    @Log(title = "饮水记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<Drink> drinkList)
    {
        if (drinkList == null || drinkList.isEmpty()) {
            return error("填写的饮水记录为空，请检查后重新填写");
        }
        return toAjax(drinkService.insertDrink(drinkList));
    }

    /**
     * 修改饮水记录
     */
    @PreAuthorize("@ss.hasPermi('manage:drink:edit')")
    @Log(title = "饮水记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<Drink> drinkList)
    {
        return toAjax(drinkService.updateDrink(drinkList));
    }

    /**
     * 删除饮水记录
     */
    @PreAuthorize("@ss.hasPermi('manage:drink:remove')")
    @Log(title = "饮水记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{waterIds}")
    public AjaxResult remove(@PathVariable Long[] waterIds)
    {
        return toAjax(drinkService.deleteDrinkByWaterIds(waterIds));
    }
}
